// 购物车模块
// import http from '@/shopro/request/index'

const state = {
	cartList: [], //购物车列表
	checkCart: {}, //检测是否是购物车数据的对象
	allSelected: false, // 全选检测
	cartNum: 0, //购物车,涉及到刷新数据丢失，所以存了本地,
}


const getters = {
	cartList: state => state.cartList,
	checkCart: state => state.checkCart,
	allSelected: state => state.allSelected,
	cartNum: state => state.cartNum,
	// 购物车数量和总价
	totalCount: state => {
		console.log(222);
		let totalNum = 0;
		let totalPrice = 0;
		state.cartList.filter(item => {
			if (item.checked) {
				totalNum += 1;
				totalPrice += item.goods_num * (item.sku_price ? item.sku_price.price : 0);
			}
		})
		return {
			totalNum,
			totalPrice
		}
	},

	// 是否选择了商品
	isSel: state => {
		let isSel = false;
		state.cartList.map(item => {
			if (item.checked) {
				isSel = true
			}
		})
		return isSel
	},

	// 活动商品唯一选中可以结算
	isActivityPay() {
		let num = 0
		let activityNum = 0
		state.cartList.map(item => {
			if (item.checked) {
				num += 1
				if (item.cart_type === 'activity') {
					activityNum += 1
				}
			}
		})
		if ((num === 1 && activityNum === 1) || activityNum === 0) {
			return true
		}
		return false
	}

}


const mutations = {
	// cart数据获取变动。
	CART_LIST(state, payload) {
		state.cartList = payload;
		let cartListNumber = payload.length;

		state.cartNum = cartListNumber;
	},

	// 切换全选。
	changeAllSellect(state) {
		state.allSelected = !state.allSelected;
	},
	// 全选设置
	getAllSellectCartList(state, flag) {
		state.cartList.map(item => {
			item.checked = flag
		})
	},
	// 全选检测
	checkCartList(state) {
		let all = true;
		state.cartList.map(item => {
			if (!item.checked) {
				all = false
			}
		})
		state.allSelected = all;
	},
	// 是否购物车数据检测
	getCheckCart(state) {
		let obj = {};
		state.cartList.map(item => {
			obj[item.goods_id] = {
				num: item.goods_num,
				cartOrderId: item.id
			};
		});
		state.checkCart = obj;
	},
}

const actions = {
	// 购物车数据（查）
	getCartList({
		commit
	}) {
		return new Promise((resolve, reject) => {
			let cartData = [{
					"id": 8505,
					"user_id": 3560,
					"goods_id": 1439,
					"goods_num": 1,
					"sku_price_id": 2402,
					"activity_type": null,
					"activity_id": null,
					"goods_ids": null,
					"cart_type": null,
					"goods": {
						"id": 1439,
						"type": "normal",
						"title": "11",
						"subtitle": "111",
						"weigh": 11,
						"category_ids": "44,44,45,44,45,46",
						"image": "https:\/\/demo.shopro.top\/uploads\/20240117\/d2a1208fddf07c421e815866532f430e.jpeg",
						"images": [
							"https:\/\/demo.shopro.top\/uploads\/20240117\/d2a1208fddf07c421e815866532f430e.jpeg"
						],
						"params": [{
							"title": "111",
							"content": "111"
						}],
						"content": "<p>111<\/p>",
						"price": "1",
						"original_price": "1.00",
						"is_sku": 0,
						"likes": 0,
						"views": 1524,
						"sales": 0,
						"show_sales": 1111,
						"service_ids": "30",
						"dispatch_type": "express",
						"dispatch_ids": "1",
						"deletetime": null,
						"dispatch_type_arr": [
							"express"
						]
					},
					"sku_price": {
						"id": 2402,
						"goods_sku_ids": null,
						"goods_id": 1439,
						"weigh": 0,
						"image": null,
						"stock": 1,
						"stock_warning": null,
						"sales": 0,
						"sn": "",
						"weight": 0,
						"price": "1.00",
						"goods_sku_text": null,
						"status": "up",
						"goods_sku_id_arr": [
							""
						]
					}
				},
				{
					"id": 8506,
					"user_id": 3560,
					"goods_id": 1433,
					"goods_num": 1,
					"sku_price_id": 2385,
					"activity_type": null,
					"activity_id": null,
					"goods_ids": null,
					"cart_type": null,
					"goods": {
						"id": 1433,
						"type": "normal",
						"title": "测试1114",
						"subtitle": "测试1114",
						"weigh": 2,
						"category_ids": "44,44,45,44,45,46",
						"image": "https:\/\/demo.shopro.top\/uploads\/20231114\/cf85108151291911df42636029a8a9ae.jpg",
						"images": [
							"https:\/\/demo.shopro.top\/uploads\/20231114\/cf85108151291911df42636029a8a9ae.jpg"
						],
						"params": [{
							"title": "1",
							"content": "1"
						}],
						"content": "<p>130<br><\/p>",
						"price": "100",
						"original_price": "200.00",
						"is_sku": 1,
						"likes": 0,
						"views": 282,
						"sales": 0,
						"show_sales": 1,
						"service_ids": "29",
						"dispatch_type": "express",
						"dispatch_ids": "1",
						"deletetime": null,
						"dispatch_type_arr": [
							"express"
						]
					},
					"sku_price": {
						"id": 2385,
						"goods_sku_ids": "1684,1689",
						"goods_id": 1433,
						"weigh": 0,
						"image": "",
						"stock": 33,
						"stock_warning": null,
						"sales": 0,
						"sn": "",
						"weight": 0,
						"price": "130.00",
						"goods_sku_text": "红色,25",
						"status": "up",
						"goods_sku_id_arr": [
							"1684",
							"1689"
						]
					}
				},
				{
					"id": 8507,
					"user_id": 3560,
					"goods_id": 1449,
					"goods_num": 1,
					"sku_price_id": 2425,
					"activity_type": null,
					"activity_id": null,
					"goods_ids": null,
					"cart_type": null,
					"goods": {
						"id": 1449,
						"type": "normal",
						"title": "111",
						"subtitle": "111",
						"weigh": 1,
						"category_ids": "46",
						"image": "https:\/\/demo.shopro.top\/uploads\/20240404\/f378e4cc11ee5baec4654cd996dccfa9.jpg",
						"images": [
							"https:\/\/demo.shopro.top\/uploads\/20240404\/f378e4cc11ee5baec4654cd996dccfa9.jpg"
						],
						"params": [],
						"content": "",
						"price": "22",
						"original_price": "121.00",
						"is_sku": 0,
						"likes": 0,
						"views": 244,
						"sales": 0,
						"show_sales": 212,
						"service_ids": "",
						"dispatch_type": "express",
						"dispatch_ids": "26",
						"deletetime": null,
						"dispatch_type_arr": [
							"express"
						]
					},
					"sku_price": {
						"id": 2425,
						"goods_sku_ids": null,
						"goods_id": 1449,
						"weigh": 0,
						"image": null,
						"stock": 212,
						"stock_warning": null,
						"sales": 0,
						"sn": "",
						"weight": 0,
						"price": "22.00",
						"goods_sku_text": null,
						"status": "up",
						"goods_sku_id_arr": [
							""
						]
					}
				}
			]
			cartData.length && cartData.map(item => {
				item.checked = true;
			})
			commit('CART_LIST', cartData);
			commit('checkCartList');
			commit('getCheckCart')
			// http('cart.index').then(res => {
			// 	if (res.code === 1) {
			// 		let cartData = res.data;
			// 		cartData.length && cartData.map(item => {
			// 			item.checked = true;
			// 		})
			// 		commit('CART_LIST', cartData);
			// 		commit('checkCartList');
			// 		commit('getCheckCart')
			// 	}

			// }).catch(e => {
			// 	reject(e)
			// })
		})
	},
	// 添加到购物车（增）
	addCartGoods({
		dispatch
	}, data) {
		return new Promise((resolve, reject) => {
			http('cart.add', {
				goods_list: data.list,
				from: data.from
			}).then(res => {
				res.code === 1 && dispatch('getCartList');
				resolve(res)
			}).catch(e => {
				reject(e)
			})
		})
	},
	// 修改购物车商品数量（改）|| 删除购物车商品（删）
	changeCartList({
		commit,
		dispatch
	}, param) {
		console.log(param);
		return new Promise((resolve, reject) => {
			http('cart.edit', {
				cart_list: param.ids,
				value: param.goodsNum || null,
				act: param.art
			}).then(res => {
				if (param.art === 'delete' && res.code === 1) {
					dispatch('getCartList');
				}
				commit('checkCartList');
				resolve(res)
			}).catch(e => {
				reject(e)
			})
		})
	},
}

export default {
	state,
	mutations,
	actions,
	getters
}